/*
 * Copyright 2011-2012 Redia A/S
 * 
 * This file is part of Redias Library App for Android.
 *
 *  Redia Library App for Android is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redia Library App for Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redia Library App for Android.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.libraryapp.activityutils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import dk.redia.libraryapp.BarcodeScanActivity;
import dk.redia.libraryapp.GlobalMenuHandler;
import dk.redia.libraryapp.R;
import dk.redia.libraryapp.ScannerIntroActivity;
import dk.redia.libraryapp.SearchResultActivity;
import dk.redia.libraryapp.search.QueryManager;
import dk.redia.libraryapp.utils.SoftKeyboardUtils;

public class LibraryAppActivity extends Activity implements OnEditorActionListener {
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
	}
	
	public void setContentView(int id) {
		super.setContentView(id);
		final EditText et = (EditText)findViewById(R.id.searchBarEditText);
		if (et!=null) {
			et.setText(QueryManager.getInstance().getSearchQuery());
			et.setOnEditorActionListener(this);

			// Make it easy to clear the search query field by selecting
			// everything inside it when it gains focus
			et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
				
				public void onFocusChange(View v, boolean hasFocus) {
					if (hasFocus) {
						et.selectAll();
					}
				}
			});
		}
		View scanButton = (Button)findViewById(R.id.searchBarScanButton);
		// Check if this activity has the scan button
		if (scanButton!=null) {
			// Check if scanning mode is enabled for this customer
			boolean enableScanner = getResources().getInteger(R.integer.enableScanner)>0;
			if (!enableScanner) {
				scanButton.setVisibility(View.GONE);
			}
		}
	}
	
	public void onSearchButtonClick(View button, boolean finishActivity) {
		EditText et = (EditText)findViewById(R.id.searchBarEditText);
		SoftKeyboardUtils.hideSoftKeyboard(et);
		QueryManager.getInstance().setSearchQuery(et.getText().toString());
		Intent intent = new Intent(this, SearchResultActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		if (finishActivity) {
			finish();
		}
	}
	
	public void onScanButtonClick(View button) {
		Intent i;
		if (ScannerIntroActivity.skipIntro()) {
			i = new Intent(this, BarcodeScanActivity.class );
		} else {
			i = new Intent(this, ScannerIntroActivity.class);
		}
			
		startActivity(i);
	}
	
/*	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
		if (scanResult != null) {
			Log.d("Scanner", scanResult.getFormatName());			
			if (scanResult.getFormatName().equals("QR_CODE")) {
				// Verify that associated data contains a URL
				Log.d("Scanner", scanResult.getRawBytes().toString());
				// Open a browser showing the URL
			} else {
				// FIXME Assume that we can translate a barcode number to a faust id
				// FIXME Actually retrieve a SearchResultDataItem from the web service
				Intent i = new Intent(this, SearchResultDetailsActivity.class);
				SearchResultDataItem srdi = new SearchResultDataItem();
				srdi.setAbstract("Abstract");
				srdi.setCoverId("coverid");
				srdi.setCreator("Creator");
				srdi.setDate("In a year or two");
				srdi.setIdentifier("identifier");
				srdi.setTitle("Title");
				srdi.setType("Something or other");
				i.putExtra(SearchResultDetailsActivity.extraKeySearchResultDataItem, srdi);
				startActivity(i);
			}
		}
	}
*/	
	public void onSearchButtonClick(View button) {
		onSearchButtonClick(button, true);
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		return onOptionsItemSelected(item, true);
	}
	
	public boolean onOptionsItemSelected(MenuItem item, boolean finish) {		
		boolean result = GlobalMenuHandler.onOptionsItemSelected(this, item);
		if (finish) {			
			finish();
		}
		return result;
	}

	public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
		onSearchButtonClick(null);
		return true;
	}
}
